/**
 * create by young (young2f@outlook.com) on 2020/12/4
 */
'use strict';
const base = require('../base/serviceBase');
const _ = require('lodash');
const fs = require('fs');
const _stringUtils = require('../utils/stringUtils');
// 1  引入formstream

module.exports = class extends base {
  async getModel(body) {
    const { ctx } = this;
    const { tenantCode, table } = body;

    const mod = ctx.model[table.charAt(0)
      .toUpperCase() + table.slice(1)];
    console.log('get model', mod);
    const schema = mod.schema.obj;
    delete schema['tenantName'];
    delete schema['tenantCode'];
    delete schema['createUser'];
    delete schema['createUsername'];
    delete schema['isDeleted'];
    delete schema['isEnabled'];
    delete schema['labels'];
    //console.log('schemaa', schema);
    _.map(schema, item => {
      // const {type}=item;
      // item.inputType = item.type==='boolean'?'Switch':type;
      item.label = item.title;
    });
    const dLabels = await ctx.model.DLabel.find({ tenantCode, table });
    schema.labels = dLabels;
    if (table === 'SalesOrder') {
      const prods = {
        prodName: { type: 'string', inputType: 'input', desc: '商品品名', enableArea: [ 'search' ] },
        prodCode: { type: 'string', inputType: 'input', desc: '商品编号', enableArea: [ 'search' ] },
        count: { type: 'string', inputType: 'inputNumber', desc: '商品重量（KG)', enableArea: [ 'search' ] },
        price: { type: 'string', inputType: 'inputNumber', desc: '商品单价', enableArea: [ 'search' ] },
        label: { type: 'string', inputType: 'input', desc: '商品标签', enableArea: [ 'search' ] },
        pkg: { type: 'string', inputType: 'input', desc: '商品包装', enableArea: [ 'search' ] },
        pkgFee: { type: 'string', inputType: 'inputNumber', desc: '商品包装费用', enableArea: [ 'search' ] },
        laborFee: { type: 'string', inputType: 'inputNumber', desc: '商品劳务费用', enableArea: [ 'search' ] },
        taxDeductionPointForLaborCosts: {
          type: 'string',
          inputType: 'inputNumber',
          desc: '商品劳务扣税点',
          enableArea: [ 'search' ]
        },
        deliverWay: {
          type: 'string', inputType: 'select', desc: '商品发货方式', enableArea: [ 'search' ], option: [
            '直发',
            '仓库',
            '业务员送',
            '客户自提'
          ]
        },
        shippingMan: { type: 'string', inputType: 'select', desc: '商品送货渠道', enableArea: [ 'search' ] },
        invoicingDate: { type: 'string', inputType: 'date', desc: '商品开票日期', enableArea: [ 'search' ] },
      };
      const materials = {
        material: { type: 'string', inputType: 'input', desc: '原料名称', enableArea: [ 'search' ] },
        materialCode: { type: 'string', inputType: 'input', desc: '原料编号', enableArea: [ 'search' ] },
        materialCount: { type: 'string', inputType: 'inputNumber', desc: '原料数量', enableArea: [ 'search' ] },
        materialContent: { type: 'string', inputType: 'input', desc: '原料含量', enableArea: [ 'search' ] },
        subcategory: { type: 'string', inputType: 'input', desc: '原料小类', enableArea: [ 'search' ] },
        cost: { type: 'string', inputType: 'inputNumber', desc: '成本', enableArea: [ 'search' ] },
      };
      prods.materials = materials;
     // schema.prods = prods;
    }
   // console.log('sss',schema)
    return schema;
  }


  async uploadPDF(body) {
    // 获取之前的 header
    const { ctx } = this;
    const { query, files } = body;
    const { table, orderCode, tenantCode, fileList } = query;

    //const _order = await ctx.app.dbs.findOne(table, { code: orderCode });
    // const order = _order ? _order : {};
    // console.log('order', order);
    // const { pdfFileList = [] } = order;
    // 遍历处理多个文件
    const pdfFileList = [];
    // for (const file of ctx.request.files) {
    //   console.log('filename: ' + file.filename);
    //   console.log('tmp filepath: ' + file.filepath);
    //   const filename=file.filepath.replace("D:\\iiot\\code\\clf-pmc-server\\", '');
    //   console.log('filename: ' + filename);
    //   pdfFileList.push(file);
    // }
    pdfFileList.push(ctx.request.files[0]);
    // const result = _order ? await ctx.app.dbs.updateOne(table, { code: orderCode }, { pdfFileList:fileList }) : await ctx.app.dbs.insert(table, {
    //   tenantCode,
    //   code: orderCode,
    //   pdfFileList:fileList
    // });
    console.log('tmp result: ', pdfFileList);
    return {
      status: 200,
      result: { url: `http://127.0.0.1:18008/api/global/download?file=${ctx.request.files[0].filepath}` }
    };
  }
};
